using AiQiuQuan.Sport.Model.Enums;
using System.ComponentModel.DataAnnotations;

namespace AiQiuQuan.Sport.Model.Models
{
    /// <summary>
    /// 新增课程dto
    /// </summary>
    public class CourseAddDto : IValidatableObject
    {
        /// <summary>
        /// 课程名称
        /// </summary>
        [Required(AllowEmptyStrings = false, ErrorMessage = "课程名称必填")]
        public string Name { get; set; }

        /// <summary>
        /// 1次数课程、2期间课程、3活动课程
        /// </summary>
        [Required(ErrorMessage = "1次数课程、2期间课程、3活动课程必填")]
        [EnumDataType(typeof(CourseTypeEnum))]
        public CourseTypeEnum CourseType { get; set; }

        /// <summary>
        /// 课程图片
        /// </summary>
        [Required]
        public string HeadUrl { get; set; }

        /// <summary>
        /// 场馆ID
        /// </summary>
        public Guid? VenueId { get; set; }

        /// <summary>
        /// 地点名称
        /// </summary>
        public string? Address { get; set; }

        /// <summary>
        /// 省ID
        /// </summary>
        public Guid? ProvinceId { get; set; }

        /// <summary>
        /// 省名
        /// </summary>
        public string? ProvinceName { get; set; }

        /// <summary>
        /// 市ID
        /// </summary>
        public Guid? CityId { get; set; }

        /// <summary>
        /// 市名
        /// </summary>
        public string? CityName { get; set; }

        /// <summary>
        /// 经度
        /// </summary>
        public decimal? Lng { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        public decimal? Lat { get; set; }

        /// <summary>
        /// 课程介绍
        /// </summary>
        [Required]
        public string Introduction { get; set; }

        /// <summary>
        /// 课程明细
        /// </summary>
        [MinLength(1)]
        [Required]
        public List<CourseDetailAddDto> DetailList { get; set; }

        /// <summary>
        /// 课程教练
        /// </summary>
        [MinLength(1)]
        [Required]
        public List<CourseCoacherAddDto> CoacherList { get; set; }

        /// <summary>
        /// 课程计划
        /// </summary>
        [MinLength(1)]
        [Required]
        public List<CoursePlanAddDto>? PlanList { get; set; }

        /// <summary>
        /// valid
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!VenueId.HasValue || VenueId.Value == default)
            {
                if (string.IsNullOrEmpty(ProvinceName) || !ProvinceId.HasValue || VenueId.Value == default)
                {
                    yield return new ValidationResult("省市必填");
                    yield break;
                }

                if (string.IsNullOrEmpty(Address))
                {
                    yield return new ValidationResult("地点必填");
                    yield break;
                }

                CityId ??= ProvinceId;
                CityName ??= string.Empty;
                Lng ??= 0;
                Lat ??= 0;
            }

            if (CourseType == CourseTypeEnum.Times)
            {
                if (DetailList.Any(m => m.TimesCount <= 0 || m.ValidMonth <= 0 || m.Price <= 0))
                {
                    yield return new ValidationResult("次数课数据有误");
                    yield break;
                }
            }
            else if (CourseType == CourseTypeEnum.Duration)
            {
                if (DetailList.Any(m => m.ValidMonth <= 0 || m.Price <= 0))
                {
                    yield return new ValidationResult("期间课数据有误");
                    yield break;
                }
            }
            else
            {
                if (DetailList.Any(m => !m.StartTime.HasValue || !m.EndTime.HasValue || m.Price <= 0))
                {
                    yield return new ValidationResult("活动课数据有误");
                    yield break;
                }

                if (DetailList.Any(m => m.StartTime.Value.Date < DateTimeOffset.Now.Date))
                {
                    yield return new ValidationResult("活动课开始日期小于当前日期");
                    yield break;
                }

                if (DetailList.Any(m => m.StartTime > m.EndTime))
                {
                    yield return new ValidationResult("活动课开始日期大于结束日期");
                    yield break;
                }
            }

            foreach (var item in CoacherList)
            {
                if (CoacherList.Count(m => m.CoacherUserId == item.CoacherUserId) >= 2)
                {
                    yield return new ValidationResult("教练重复");
                    yield break;
                }
            }

            foreach (var plan in PlanList)
            {
                if (PlanList.Count(m => m.WeekDay == plan.WeekDay) >= 2)
                {
                    yield return new ValidationResult("课程计划周重复");
                    yield break;
                }

                if (!TimeSpan.TryParse(plan.StartTime, out var s) || !TimeSpan.TryParse(plan.EndTime, out var e))
                {
                    yield return new ValidationResult("课程计划时间格式错误");
                    yield break;
                }

                if (s > e)
                {
                    yield return new ValidationResult("课程计划开始时间大于结束时间");
                    yield break;
                }
            }
        }
    }
}
